package com.hmyd.hmyd_android.ui;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.crypto.Crypto;
import com.hmyd.hmyd_android.dto.AppSystem;
import com.hmyd.hmyd_android.dto.CurrentTimeMillis;
import com.hmyd.hmyd_android.dto.Key;
import com.hmyd.hmyd_android.dto.User;
import com.hmyd.hmyd_android.utils.PreferencesUtils;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

public class TransitionActivity extends BasicActivity {
	private final int LOGIN = 1;
	private final int CURRENTTIMEMILLIS = 3;// 获取服务器当前时间
	private final int KEY = 4;// 获取密匙
	private final int ROOT = 5;// 系统配置
	private String mobile;
	private String pwd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View view = new View(this);
		
		setContentView(view);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		String timestamp = String.valueOf(System.currentTimeMillis() + 
				PreferencesUtils.getLong(this, PreferencesUtils.TIMEDIFFERENCE));
		if (TextUtils.isEmpty(timestamp)) {
			getCurrentTime();
		} else if(TextUtils.isEmpty(AppBasic.key)) {
			getKey();
		} else if (AppBasic.app == null) {
			getRoot();
		}else {
			login();
		}
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case CURRENTTIMEMILLIS:
			java.lang.reflect.Type currentTimeMillisType = new TypeToken<CurrentTimeMillis>() {
			}.getType();
			CurrentTimeMillis currentTimeMillis = gson.fromJson(result, currentTimeMillisType);

			PreferencesUtils.putLong(this, PreferencesUtils.TIMEDIFFERENCE,
					currentTimeMillis.timeMillis - System.currentTimeMillis());
			AppBasic.key  = null; 
			getKey();
			break;

		case KEY:
			java.lang.reflect.Type keyType = new TypeToken<Key>() {
			}.getType();
			Key key = gson.fromJson(result, keyType);

			AppBasic.key = Utils.getSignDecrypt(this, key.key);

			getRoot();
			break;

		case LOGIN:
			java.lang.reflect.Type type2 = new TypeToken<User>() {
			}.getType();
			User user = gson.fromJson(result, type2);

			AppBasic.isLogin = true;
			AppBasic.isCheckIned = user.isCheckIned;
			AppBasic.totalScore = user.score;
			AppBasic.certNo = user.certNo;
			AppBasic.realName = user.realName;
			AppBasic.avatar = user.avatar;
			AppBasic.username = user.username;
			AppBasic.nickname = user.nickname;
			AppBasic.urlToken = user.urlToken;
			AppBasic.securityLevel = user.securityLevel;
			AppBasic.isSetSecurityCode = user.isSetSecurityCode;
			AppBasic.tel = user.tel;
			AppBasic.email = user.email;
			AppBasic.isVerifiedEmail = user.isVerifiedEmail;
			AppBasic.isSetSecurityQuestion = user.isSetSecurityQuestion;
			AppBasic.unReadMessageCount = user.unReadMessageCount;
			
			AppBasic.getInstance().finishCurrentActivity();
			break;
		case ROOT:
			java.lang.reflect.Type systemType = new TypeToken<AppSystem>() {
			}.getType();
			AppSystem system = gson.fromJson(result, systemType);
			if (system != null && system.sys != null && system.sys.app != null) {
				AppBasic.app = system.sys.app;
			}
			
			login();
			break;
		default:
			break;
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
	}

	@Override
	protected void leftButton() {
	}

	@Override
	protected void rightButton() {
	}
	
	private void getCurrentTime(){
		onRequest(CURRENTTIMEMILLIS, UrlConstant.CURRENTTIMEMILLIS, HttpMethod.GET, null);
	}
	
	private void getKey(){
		onRequest(KEY, UrlConstant.KEY, HttpMethod.GET, null);
	}
	
	/**
	 * 获取登录状态及信息
	 */
	private void login() {
		if (!TextUtils.isEmpty(PreferencesUtils.getString(this, PreferencesUtils.USER_PHONE))
				&& !TextUtils.isEmpty(PreferencesUtils.getString(this,
						PreferencesUtils.USER_PASSWORD))) {
			mobile= PreferencesUtils.getString(this, PreferencesUtils.USER_PHONE);
			pwd= Crypto.decryptAES(
					PreferencesUtils.getString(this, PreferencesUtils.USER_PASSWORD),
					Crypto.PRIVATEKEY);

			RequestParams params = new RequestParams();
			params.addBodyParameter("username", mobile);
			params.addBodyParameter("password", pwd);

			onRequest(LOGIN, UrlConstant.LOGIN, HttpMethod.POST, params);
		} else {
			AppBasic.isLogin = false;
			if(!AppBasic.getInstance().isActivityLive(LoginActivity.class)) {
				startActivity(new Intent(this, LoginActivity.class));
				AppBasic.getInstance().finishCurrentActivity();
			}
		}
	}

	private void getRoot() {
		onRequest(ROOT, UrlConstant.ROOTS, HttpMethod.GET, null);
	}
	
}
